//
//  HomeViewController.swift
//  SkylowAPP
//
//  Created by 王王凤丽 on 2024/9/20.
//

import UIKit

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
       
        setupUI()
        
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_nav_search"), style: .plain, target: self, action: #selector(leftBtnClick))
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_nav_more"), style: .plain, target: self, action: #selector(rightBtnClick))
    }
    @objc func createCharacter() {
        let createVC = CharacterCreateStepViewController()
        let nav = SWNavigationController(rootViewController: createVC)
        nav.modalPresentationStyle = .fullScreen
        present(nav, animated: true)
    }
    
    @objc func leftBtnClick(){
        let searchVC = SearchViewController()
        self.navigationController?.pushViewController(searchVC, animated: true)
    }
    
    @objc func rightBtnClick(){
        
    }
    
    // MARK: - View
    fileprivate func setupUI() {
      view.backgroundColor = UIColor.colorWith(hexString: "#333333")
        
    self.navigationItem.title = "Skylow"
        
      let layout = SLWaterfallFlowLayout()
      layout.delegate = self
      let collection = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
      collection.backgroundColor =  UIColor.colorWith(hexString: "#333333")
      collection.delegate = self
      collection.dataSource = self
     let nibCell = UINib(nibName: "HomeItemCell", bundle: nil)
        collection.register(nibCell, forCellWithReuseIdentifier: HomeItemCell.identifiers)
        
      view.addSubview(collection)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    // MARK: - ====== 私有方法 ======
   
}

extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
  func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 20
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomeItemCell.identifiers, for: indexPath) as! HomeItemCell
    return cell
  }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = HomeScreenViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}



extension HomeViewController: SLWaterfallFlowLayoutDelegate {
  func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
      return CGFloat((arc4random() % 2 + 1) * 20 + 220)
  }
  
  func columnNumber(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> Int {
      return 2
  }
  
  func insetForSection(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
  }
  
  
  func lineSpacing(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 5
  }
  
  func interitemSpacing(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 5
  }
  
  func spacingWithLastSection(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 0
  }
}


